package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.entity.HouseBean;
import com.qiantu.api.db.greendao.HouseBeanDao;

import java.util.List;

/**
 * Created by cjc on 2019/5/7
 */
public class LLHouseDBManager {

    private final String TAG = "LLHouseDBManager";
    private volatile static LLHouseDBManager instance = null;
    private final static Object object = new Object();
    private final HouseBeanDao mHouseBeanDao;
    private Context context;

    private LLHouseDBManager(Context context) {
        this.context = context;
        mHouseBeanDao = LocalDataBase.getInstance().getDaoSession(context).getHouseBeanDao();
    }

    public static LLHouseDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLHouseDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mHouseBeanDao.deleteAll();
    }

    /**
     * 从服务器更新房屋数据
     *
     * @param houseList
     */
    public void putInTx(List<HouseBean> houseList) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (HouseBean house : houseList) {
                put(house);
            }
        });
    }

    /**
     * 从服务器更新房屋数据
     *
     * @param houseList
     */
    public void refreshAllHousesTx(List<HouseBean> houseList, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(houseList == null){
                return;
            }
            Log.i("lzxzx", "refreshAllHousesTx:" + houseList.size());
            for (HouseBean house : houseList) {
                mHouseBeanDao.save(house);
            }
        });
    }

    /**
     * 保存或者更新房屋
     *
     * @param house
     */
    public void put(HouseBean house) {
        if (house == null)
            return;
        String houseSerialNo = house.getHouseSerialNo();
        if (houseSerialNo != null && !TextUtils.isEmpty(houseSerialNo)) {
            HouseBean oldHouse = mHouseBeanDao.queryBuilder()
                    .where(HouseBeanDao.Properties.HouseSerialNo.eq(houseSerialNo))
                    .unique();
            if (oldHouse != null) {
                house.setId(oldHouse.getId());
            }
        }
        mHouseBeanDao.save(house);
    }

    /**
     * 通过houseSerialNo获取房间
     *
     * @param houseSerialNo
     * @return
     */
    public HouseBean getHouseByHouseSerialNo(String houseSerialNo) {
        return mHouseBeanDao.queryBuilder().where(HouseBeanDao.Properties.HouseSerialNo.eq(houseSerialNo)).unique();
    }

    /**
     * 删除房屋
     *
     * @param houseSerialNo
     */
    public void remove(String houseSerialNo) {
        if (TextUtils.isEmpty(houseSerialNo))
            return;
        HouseBean tbHouse = mHouseBeanDao.queryBuilder()
                .where(HouseBeanDao.Properties.HouseSerialNo.eq(houseSerialNo))
                .unique();
        if (tbHouse != null) {
            mHouseBeanDao.delete(tbHouse);
        }
    }


    /**
     * 获得本地所有房屋列表
     *
     * @return
     */
    public List<HouseBean> getAll() {
        return mHouseBeanDao.queryBuilder()
                .orderAsc(HouseBeanDao.Properties.CreateTime)
                .list();
    }

    public boolean hasTransferringHouses() {
        long count = mHouseBeanDao.queryBuilder()
                .where(HouseBeanDao.Properties.Type.eq(1), HouseBeanDao.Properties.RoleType.eq(0))
                .count();
        return count > 0 ? true : false;
    }

    /**
     * 获得房屋信息
     *
     * @return
     */
    public HouseBean get(String houseSerialNo) {
        return mHouseBeanDao.queryBuilder()
                .where(HouseBeanDao.Properties.HouseSerialNo.eq(houseSerialNo))
                .unique();
    }
}
